package com.janlei.tdd.lintcode.t_1910;

import lombok.AllArgsConstructor;

import java.util.*;

import static java.util.Objects.requireNonNull;

public class T_1910 {


    int findNumber(int[] nums) {
        requireNonNull(nums);
        Map<Integer, Integer> map = new HashMap<>(16);
        Arrays.stream(nums)
                .forEach(num ->
                        map.compute(num, (key, value) -> value != null ? value + 1 : 1));

        List<Node> content = new ArrayList<>();
        map.forEach((k, v) -> content.add(new Node(k, v)));

        Node node = content.stream()
                .min((o1, o2) -> o1.time == o2.time ?
                        Integer.compare(o1.id, o2.id) : Integer.compare(o2.time, o1.time))
                .orElseThrow(IllegalAccessError::new);

        return node.id;
    }
}

@AllArgsConstructor
class Node {
    int id;
    int time;
}